﻿using Enter.Utils.RandomExt;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Enter.Utils.CryptoExt
{
    public class Token
    { 
        public string Id { get; set; }
        public DateTime ExpirTime { get; set; }
        public long UserId { get; set; }

        
        public override string ToString()
        {
            var str = string.Format("{0}.{1}.{2}", Id, ExpirTime.ToTimestamp(), UserId);
            return RSAExtend.Encrypt(str);
        }
    }

    public static class TokenExtend
    {
        public static Token NewToken(long userId)
        {
            return new Token
            {
                Id = RandomExtend.NewGuid(),
                ExpirTime = DateTime.Now.Add(Settings.CacheTimeExpiration),
                UserId = userId,
            };
        }

        public static Token Pase(string token)
        {
            var tokens = RSAExtend.Decrypt(token).Split(".");
            return new Token
            {
                Id = tokens[0],
                ExpirTime = DateTimeExtend.ConvertToDateTime(tokens[1]),
                UserId = long.Parse(tokens[2]),
            };
        }

    }
}
